package com.lib.net

import com.google.gson.GsonBuilder
import com.lib.base.BuildConfig
import com.lib.base.base.BaseApplication
import com.lib.net.interceptor.CookieInterceptor
import com.lib.net.interceptor.NetWorkInterceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory

object ApiEngine {

    private var mHost = BuildConfig.TEST_URL

    private var mRetrofitBuild: Retrofit.Builder

    private var mRetrofit: Retrofit

    private var mOKBuilder: OkHttpClient.Builder

    private val gsonConverterFactory =
        GsonConverterFactory.create(GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create())

    init {
        mRetrofitBuild = Retrofit.Builder()
            .baseUrl(mHost)
            .addConverterFactory(gsonConverterFactory)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        mOKBuilder = OkHttpClient.Builder()
            .addInterceptor(CookieInterceptor())
            .addNetworkInterceptor(NetWorkInterceptor())
            .addInterceptor(HttpLoggingInterceptor().apply { this.level = HttpLoggingInterceptor.Level.BODY })


        mRetrofit = mRetrofitBuild.client(mOKBuilder.build()).build()

    }

    fun <T> createService(service: Class<T>): T {
        return mRetrofit.create(service)
    }

}